package net.cloudcodex.client.home.presenter;

import java.util.ArrayList;
import java.util.List;

import net.cloudcodex.client.CloudCodexEventBus;
import net.cloudcodex.client.character.view.panel.CharacterPanel;
import net.cloudcodex.client.home.view.HomeCharacterView;
import net.cloudcodex.client.home.view.HomeCharacterViewImpl;
import net.cloudcodex.client.home.view.HomeView.Message;
import net.cloudcodex.client.home.view.panel.character.HomeCharacterPanel;
import net.cloudcodex.client.widgets.CharacterAvatar;
import net.cloudcodex.shared.dto.NotificationDTO;
import net.cloudcodex.shared.dto.home.CampaignPublicSummaryDTO;
import net.cloudcodex.shared.dto.home.CharacterSummaryDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * 
 * 
 * @author ADaroussin
 */
@Presenter(view = HomeCharacterViewImpl.class, multiple = true)
public class HomeCharacterPresenter extends
		BasePresenter<HomeCharacterView, CloudCodexEventBus> {
	private final Message message;
	private CharacterSummaryDTO characterSummaryDTO;

	@Inject
	public HomeCharacterPresenter(Message pMessage) {
		this.message = pMessage;
	}

	@Override
	public void bind() {
		this.view.getCharacterAvatar().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				eventBus.selectCharacterTab(characterSummaryDTO.getId(),
						CharacterPanel.CHARACTER_SHEET);
			}
		});

		this.view.getSeeMoreAnchor().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				eventBus.selectCharacterTab(characterSummaryDTO.getId(),
						CharacterPanel.STORYBOARD);
			}
		});

	}

	public void addCharacterSummary(CharacterSummaryDTO characterSummaryDTO) {
		this.characterSummaryDTO = characterSummaryDTO;
	}

	public void initView() {

		view.addCharacter(characterSummaryDTO.getId(),
				characterSummaryDTO.getIcon(), characterSummaryDTO.getName());

		CampaignPublicSummaryDTO campaignDTO = characterSummaryDTO
				.getCampaign();
		view.addCampaignInfos(campaignDTO.getName(), campaignDTO.getGame(),
				campaignDTO.getCreationDate(),
				campaignDTO.getGameMasterNickname());

		List<NotificationDTO> characterNotificationDTOs = characterSummaryDTO
				.getNotifications();
		if (characterNotificationDTOs != null) {
			for (NotificationDTO notificationDTO : characterNotificationDTOs) {
				view.addNotification(notificationDTO.getMessage(),
						notificationDTO.getDate());
			}
		}
		List<NotificationDTO> campaignNotificationDTOs = characterSummaryDTO
				.getCampaign().getNotifications();
		if (campaignNotificationDTOs != null) {
			for (NotificationDTO notificationDTO : campaignNotificationDTOs) {
				view.addNotification(notificationDTO.getMessage(),
						notificationDTO.getDate());
			}
		}

		view.addStoryBoardResponse(new ArrayList<String>());

		view.initView();

	}

}
